package com.ruoyi.biz.letter.controller;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.biz.letter.export.BizMayorHotlineExport;
import com.ruoyi.biz.letter.util.AcceptDeptUtil;
import com.ruoyi.biz.letter.util.CategoryUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.letter.domain.BizMayorHotline;
import com.ruoyi.biz.letter.service.IBizMayorHotlineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 政务服务热线服务Controller
 *
 * @author Fei.Cong
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/mayor/hotline")
public class BizMayorHotlineController extends BaseController {

    @Autowired
    private IBizMayorHotlineService bizMayorHotlineService;

    /**
     * 查询政务服务热线服务列表
     */
    @PreAuthorize("@ss.hasPermi('mayor:hotline:list')")
    @GetMapping("/list")
    public AjaxResult list(BizMayorHotline bizMayorHotline) {
        LambdaQueryWrapper<BizMayorHotline> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return success(bizMayorHotlineService.list(lambdaQueryWrapper));
    }

    /**
     * 分页查询政务服务热线服务列表
     */
    @PreAuthorize("@ss.hasPermi('mayor:hotline:list')")
    @GetMapping("/page")
    public TableDataInfo page(BizMayorHotline bizMayorHotline) {
        CategoryUtil.handleQueryCategoryId(bizMayorHotline);
        AcceptDeptUtil.handleQueryAcceptDept(bizMayorHotline);
        return getDataTable(bizMayorHotlineService.pageList(bizMayorHotline, startPage()));
    }

    /**
     * 导出政务服务热线服务列表
     */
    @PreAuthorize("@ss.hasPermi('mayor:hotline:export')")
    @Log(title = "政务服务热线服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizMayorHotline bizMayorHotline) {
        List<BizMayorHotline> list = bizMayorHotlineService.queryList(bizMayorHotline);
        List<BizMayorHotlineExport> exportList = list.stream().map(v -> {
            BizMayorHotlineExport export = new BizMayorHotlineExport();
            BeanUtils.copyProperties(v, export);
            export.setFirstCategoryName(Optional.ofNullable(v.getPCategoryId()).map(val -> String.valueOf(val)).orElse(null));
            export.setSecondCategoryName(Optional.ofNullable(v.getSCategoryId()).map(val -> String.valueOf(val)).orElse(null));
            return export;
        }).collect(Collectors.toList());
        ExcelUtil<BizMayorHotlineExport> util = new ExcelUtil<BizMayorHotlineExport>(BizMayorHotlineExport.class);
        util.exportEasyExcel(response, exportList, "政务服务热线服务数据");
    }

    /**
     * 获取政务服务热线服务详细信息
     */
    @PreAuthorize("@ss.hasPermi('mayor:hotline:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BizMayorHotline bizMayorHotline = bizMayorHotlineService.getById(id);
        CategoryUtil.getCategoryIds(bizMayorHotline);
        AcceptDeptUtil.getAcceptDept(bizMayorHotline);
        return success(bizMayorHotline);
    }

    /**
     * 新增政务服务热线服务
     */
    @PreAuthorize("@ss.hasPermi('mayor:hotline:add')")
    @Log(title = "政务服务热线服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMayorHotline bizMayorHotline) {
        return toAjax(bizMayorHotlineService.save(bizMayorHotline));
    }

    /**
     * 修改政务服务热线服务
     */
    @PreAuthorize("@ss.hasPermi('mayor:hotline:edit')")
    @Log(title = "政务服务热线服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMayorHotline bizMayorHotline) {
        CategoryUtil.buildCategoryId(bizMayorHotline);
        AcceptDeptUtil.buildAcceptDept(bizMayorHotline);
        return toAjax(bizMayorHotlineService.updateById(bizMayorHotline));
    }

    /**
     * 删除政务服务热线服务
     */
    @PreAuthorize("@ss.hasPermi('mayor:hotline:remove')")
    @Log(title = "政务服务热线服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizMayorHotlineService.removeBatchByIds(ids));
    }
}
